﻿/*
 *
 * Model.cs
 * For model_name.php in CI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Model, Model, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace cici
{
    class Model
    {
        private string path_application;
        private StreamWriter streamWritter;

        public Model()
        {
            this.path_application = Environment.CurrentDirectory + @"\application\models";
        }

        private void createFileModel(string file_location, string tempClassName)
        {
            this.streamWritter = new StreamWriter(file_location);
            this.streamWritter.Write(this.templateModel(file_location, tempClassName));
            this.streamWritter.Flush();
            this.streamWritter.Close();
        }

        private string[] createFolderModel(string tempClassName)
        {

            string[] temp_folder_model_split = tempClassName.Split('.');

            int length_array = temp_folder_model_split.Length - 1;

            string class_name_of_model = "Noclass";

            int className_of_index_or_array = 0;

            for (int temp_loop = length_array; temp_loop > 0; temp_loop--)
            {
                if (temp_folder_model_split[temp_loop].Length > 0)
                {
                    class_name_of_model = temp_folder_model_split[temp_loop];
                    className_of_index_or_array = temp_loop;
                    break;
                }
            }

            string requr_folder_model = "";

            for (int temp_loop2 = 0; temp_loop2 < className_of_index_or_array; temp_loop2++)
            {
                if (temp_folder_model_split[temp_loop2].Length > 0)
                {
                    requr_folder_model += @"\" + temp_folder_model_split[temp_loop2];
                }
            }
            requr_folder_model = this.path_application + requr_folder_model;

            Directory.CreateDirectory(requr_folder_model);

            return new string[] { requr_folder_model, class_name_of_model };
        }

        public Boolean createModel(string className)
        {
            Boolean status = false;

            string[] temps = className.Split(';');

            if (Directory.Exists(this.path_application))
            {
                foreach (string tempClassName in temps)
                {
                    if (tempClassName.Length > 0)
                    {
                        Match m = Regex.Match(tempClassName, @"[.]");
                        if (m.Success)
                        {
                            string[] temp_folder_and_model =
                                this.createFolderModel(tempClassName);
                            this.createFileModel(temp_folder_and_model[0] + @"\" + temp_folder_and_model[1] + ".php",
                                temp_folder_and_model[1]);

                        }
                        else
                        {
                            this.createFileModel(this.path_application + @"\" + tempClassName + ".php",
                                tempClassName);
                        }
                    }
                }

                status = true;
            }

            return status;
        }

        protected virtual string templateModel(string file_location, string className)
        {
            string tempClassName = className.Substring(0, 1);
            string realClassName = tempClassName.ToUpper() + className.Remove(0, 1);

            string template =
                "<?php\n\n" +
                "class " + realClassName + " extends CI_Model {\n\n" +
                "\t/**\n" +
                "\t * Model " + realClassName + ".\n" +
                "\t */\n" +
                "\tpublic function __construct()\n" +
                "\t{\n" +
                "\t\tparent::__construct();\n" +
                "\t}\n\n" +
                "\tpublic function __destruct()\n" +
                "\t{\n" +
                "\t\t\n" +
                "\t}\n" +
                "}\n\n" +
                "/* End of file " + className + ".php */\n" +
                "/* Location: " + file_location + " */\n" +
                "/* Generated by CICI */";

            return template;
        }
    }
}
